package cn.edu.zafu.bigdata.bd121.studentdb.command.impl;

import cn.edu.zafu.bigdata.bd121.studentdb.cli.CliMain;
import cn.edu.zafu.bigdata.bd121.studentdb.command.BaseCommand;
import cn.edu.zafu.bigdata.bd121.studentdb.entity.StudentPO;
import cn.edu.zafu.bigdata.bd121.studentdb.utils.InputUtils;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 删除学生信息命令
 *
 * @author 202211090121
 */
public class DeleteStudent extends BaseCommand {

    /**
     * 处理用户输入
     */
    @Override
    protected void handleUserInput() {
        StudentPO studentToRemove = InputUtils.getStudentPo();

        if (studentToRemove != null) {
            System.out.println("找到该学生信息：" + studentToRemove);
            if (InputUtils.confirm()) {
                try {
                    PreparedStatement statement = CliMain.connection.prepareStatement("DELETE FROM student121 WHERE id=?");
                    statement.setLong(1, studentToRemove.getId());
                    statement.executeUpdate();
                    System.out.println("学生信息已删除。");
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            } else {
                System.out.println("删除操作已取消。");
            }
        } else {
            System.out.println("找不到该学生信息，请检查输入是否正确。");
        }
    }
}
